package com.wtgroup.demo.mianshi.算法.二叉树;

import com.wtgroup.demo.common.judge.Judger;
import com.wtgroup.demo.common.util.RandomUtil;
import com.wtgroup.demo.common.util.Tools;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * 给定一棵二叉树头结点 head, 任何两个节点之间都存在距离, 返回整颗二叉树的最大距离
 * <pre>
 *                       x
 *
 *
 *                 x              x
 *
 *
 *          x           x               x
 *                                      │
 *                                      │
 *  x                x       x          │
 *  │                                   │
 *  │                                   │
 *  │                                   │
 *  └───────────────────────────────────┘
 *                        6
 * </pre>
 *
 * @author nisus
 * @version 0.1
 * @since 2023/5/3 8:44
 */
public class Q_最大距离 {

    public static void main(String[] args) {
        Judger.of(() -> {
            Node tree = generateRandomBST(RandomUtil.randomInt(0, 30), RandomUtil.randomInt());

            Info info = doit(tree);

            int refer = Tester.maxDistance(tree);

            return info.maxDistance == refer;
        }).start().print();
    }

    static Info doit(Node root) {
        if (root == null) {
           return new Info();
        }

        Info leftInfo = doit(root.left);
        Info rightInfo = doit(root.right);

        Integer currMaxDistance = Tools.max(leftInfo.maxDistance, rightInfo.maxDistance, leftInfo.maxHeight + rightInfo.maxHeight + 1);
        Integer currMaxHeight = Tools.max(leftInfo.maxHeight, rightInfo.maxHeight) + 1;
        return new Info(currMaxHeight, currMaxDistance);
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Info {
        int maxHeight;
        int maxDistance;
    }


    // for mock
    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for mock
    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    static class Tester {

        public static int maxDistance(Node head) {
            if (head == null) {
                return 0;
            }
            ArrayList<Node> arr = getPrelist(head);
            HashMap<Node, Node> parentMap = getParentMap(head);
            int max = 0;
            for (int i = 0; i < arr.size(); i++) {
                for (int j = i; j < arr.size(); j++) {
                    max = Math.max(max, distance(parentMap, arr.get(i), arr.get(j)));
                }
            }
            return max;
        }

        public static ArrayList<Node> getPrelist(Node head) {
            ArrayList<Node> arr = new ArrayList<>();
            fillPrelist(head, arr);
            return arr;
        }

        public static void fillPrelist(Node head, ArrayList<Node> arr) {
            if (head == null) {
                return;
            }
            arr.add(head);
            fillPrelist(head.left, arr);
            fillPrelist(head.right, arr);
        }

        public static HashMap<Node, Node> getParentMap(Node head) {
            HashMap<Node, Node> map = new HashMap<>();
            map.put(head, null);
            fillParentMap(head, map);
            return map;
        }

        public static void fillParentMap(Node head, HashMap<Node, Node> parentMap) {
            if (head.left != null) {
                parentMap.put(head.left, head);
                fillParentMap(head.left, parentMap);
            }
            if (head.right != null) {
                parentMap.put(head.right, head);
                fillParentMap(head.right, parentMap);
            }
        }

        public static int distance(HashMap<Node, Node> parentMap, Node o1, Node o2) {
            HashSet<Node> o1Set = new HashSet<>();
            Node cur = o1;
            o1Set.add(cur);
            while (parentMap.get(cur) != null) {
                cur = parentMap.get(cur);
                o1Set.add(cur);
            }
            cur = o2;
            while (!o1Set.contains(cur)) {
                cur = parentMap.get(cur);
            }
            Node lowestAncestor = cur;
            cur = o1;
            int distance1 = 1;
            while (cur != lowestAncestor) {
                cur = parentMap.get(cur);
                distance1++;
            }
            cur = o2;
            int distance2 = 1;
            while (cur != lowestAncestor) {
                cur = parentMap.get(cur);
                distance2++;
            }
            return distance1 + distance2 - 1;
        }
    }

}
